package com.ruoyi.tzai.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.tzai.mapper.NewsArticleMapper;
import com.ruoyi.tzai.domain.NewsArticle;
import com.ruoyi.tzai.service.INewsArticleService;
import com.ruoyi.tzai.service.INewsProcessService;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 资讯文章Service业务层处理
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Service
public class NewsArticleServiceImpl implements INewsArticleService 
{
    private static final Logger log = LoggerFactory.getLogger(NewsArticleServiceImpl.class);
    
    @Autowired
    private NewsArticleMapper newsArticleMapper;
    
    @Autowired
    private INewsProcessService newsProcessService;

    /**
     * 查询资讯文章
     * 
     * @param articleId 资讯文章主键
     * @return 资讯文章
     */
    @Override
    public NewsArticle selectNewsArticleByArticleId(Long articleId)
    {
        return newsArticleMapper.selectNewsArticleByArticleId(articleId);
    }

    /**
     * 查询资讯文章列表
     * 
     * @param newsArticle 资讯文章
     * @return 资讯文章
     */
    @Override
    public List<NewsArticle> selectNewsArticleList(NewsArticle newsArticle)
    {
        return newsArticleMapper.selectNewsArticleList(newsArticle);
    }

    /**
     * 新增资讯文章
     * 
     * @param newsArticle 资讯文章
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNewsArticle(NewsArticle newsArticle)
    {
        newsArticle.setCreateTime(DateUtils.getNowDate());
        // 默认状态：待处理
        if (newsArticle.getStatus() == null) {
            newsArticle.setStatus("0");
        }
        return newsArticleMapper.insertNewsArticle(newsArticle);
    }

    /**
     * 修改资讯文章
     * 
     * @param newsArticle 资讯文章
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNewsArticle(NewsArticle newsArticle)
    {
        newsArticle.setUpdateTime(DateUtils.getNowDate());
        return newsArticleMapper.updateNewsArticle(newsArticle);
    }

    /**
     * 批量删除资讯文章
     * 
     * @param articleIds 需要删除的资讯文章主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNewsArticleByArticleIds(Long[] articleIds)
    {
        return newsArticleMapper.deleteNewsArticleByArticleIds(articleIds);
    }

    /**
     * 删除资讯文章信息
     * 
     * @param articleId 资讯文章主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNewsArticleByArticleId(Long articleId)
    {
        return newsArticleMapper.deleteNewsArticleByArticleId(articleId);
    }

    /**
     * 处理资讯（AI+TTS）
     * 
     * @param articleId 资讯文章主键
     * @return 结果
     */
    @Override
    public int processNewsArticle(Long articleId)
    {
        log.info("触发资讯处理 - ArticleID: {}", articleId);
        
        // 异步调用资讯处理服务
        newsProcessService.processArticle(articleId);
        
        log.info("资讯处理已提交到异步队列 - ArticleID: {}", articleId);
        return 1;
    }
}

